<?php
/*
天气管理
*/
class WeatherController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		$this->commonAction();	
	}
	
	//
	
	public function indexAction(){
		try {
			$count = 30;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    			$offset = ($curpage - 1) * $count; 
			
			
			$sql="select cityid,cityname from weather_city ";
			
			$citylist = $this->_dbAdapter->fetchAssoc ( $sql );//查询全部校区
			$this->view->citylist= $citylist; 

			
			$result = $this->_dbAdapter->fetchAll ("select SQL_CALC_FOUND_ROWS *  from weather_v GROUP BY weatherid order by addtime desc limit $offset,$count");
			
			$this->view->result= $result;
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	public function deleteAction(){
		try {
			$qdate=date('Ymd',strtotime('-1 week'));//一周以前日期
			$del_result = $this->_dbAdapter->delete ( 'weather', 'day<' . $qdate );
			if ($del_result ) {
				$this->feedback ( 'success', 'success', '/'.$this->controller, 'tip', 3 );
			}else{
				$this->feedback ( 'dataclear1', 'note', '/'.$this->controller, 'tip', 3 );
			}
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//手动刷新天气
	public function opupdateAction() {
		try {
			//
			$res=$this->update();
			if($res)
				$this->feedback ('刷新天气成功,请刷新页面查看', 'note', '/'.$this->controller, 'warning' ,3);
			else
				throw new Exception ( $res );	

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//自动刷新天气
	public function autoAction() {
		try {
			//
			
			$this->update();
			exit;
		} catch ( Exception $e ) {
			exit;
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	
	
//刷新天气
	private function update() {
		try {
			//$pmurl='http://pm25.in/api/querys/pm2_5.json?city=北京&token=gpAxKiWRQeBFJ7m817Qp&stations=no';//查询空气质量
			//$pm25=json_decode(file_get_contents('http://pm25.in/api/querys/pm2_5.json?city=北京&token=gpAxKiWRQeBFJ7m817Qp&stations=no'));
			//echo substr($pm25[0]->time_point, 11,5);exit;
			$sql="select cityid,cityname from weather_city ";
			$citylist = $this->_dbAdapter->fetchAll ( $sql );//查询全部校区
			
			if(!$citylist)
				throw new Exception ( 'nodata' );
				
			$nowtime=time();			
			$is=0;
			$is1=0;
			foreach($citylist as $k => $v){
				//echo $v['cityname'];exit;
				$url = 'http://www.weather.com.cn/data/cityinfo/'.$v['cityid'].'.html';//查询天气
				$content=file_get_contents($url);
				if($content){
					$weather = json_decode($content);
	    		}
				
	    		if(!$weather)
					continue;					
				if(strtotime(date('Y-m-d '.$weather->weatherinfo->ptime))> $nowtime)
					break;
				
				$sql='insert ignore weather (cityid,temp1,temp2,weather,img1,img2,ptime,day,addtime) values ("'.$v['cityid'].'","'.$weather->weatherinfo->temp1.'","'.$weather->weatherinfo->temp2.'","'.$weather->weatherinfo->weather.'","'.$weather->weatherinfo->img1.'","'.$weather->weatherinfo->img2.'","'.$weather->weatherinfo->ptime.'","'.date('Ymd',$nowtime).'","'.$nowtime.'")';
				$pmurl='http://pm25.in/api/querys/pm2_5.json?city='.$v['cityname'].'&token=5j1znBVAsnSf5xQyNQyq&stations=no';//查询空气质量
				$pm25=json_decode(file_get_contents($pmurl));
				$result2 = $this->_dbAdapter->query ($sql);//插入
				if(empty($pm25->error)){
					if($pm25){
						echo $pm25[0]->pm2_5; 
						$sql='insert weather_pm (cityid,ptime,day,addtime,pm2_5,quality) values ("'.$v['cityid'].'","'.substr($pm25[0]->time_point, 11,5).'","'.date('Ymd',$nowtime).'","'.$nowtime.'","'.$pm25[0]->pm2_5.'","'.$pm25[0]->quality.'")';
						$result1 = $this->_dbAdapter->query ($sql);//插入
					}
				}
				
				
				if($result2)
					$is=1;
				
			}
			$nowdate=date('Ymd');
			if($is)
				$this->feedback ('刷新天气成功,请刷新页面查看', 'note', '/'.$this->controller, 'warning' ,3);
				//刷新天气成功
			else
				throw new Exception ( 'is_news' );

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//刷新天气ss
	public function updateAction() {
		try {
			//$pmurl='http://pm25.in/api/querys/pm2_5.json?city=北京&token=gpAxKiWRQeBFJ7m817Qp&stations=no';//查询空气质量
			//$pm25=json_decode(file_get_contents('http://pm25.in/api/querys/pm2_5.json?city=北京&token=gpAxKiWRQeBFJ7m817Qp&stations=no'));
			//echo substr($pm25[0]->time_point, 11,5);exit;
			$sql="select cityid,cityname from weather_city ";
			$citylist = $this->_dbAdapter->fetchAll ( $sql );//查询全部校区
			
			if(!$citylist)
				throw new Exception ( 'nodata' );
				
			$nowtime=time();			
			$is=0;
			$is1=0;
			foreach($citylist as $k => $v){
				//echo $v['cityname'];exit;
				$url = 'http://www.weather.com.cn/data/cityinfo/'.$v['cityid'].'.html';//查询天气
				$content=file_get_contents($url);
				if($content){
					$weather = json_decode($content);
	    		}
				
	    		if(!$weather)
					continue;					
				//if(strtotime(date('Y-m-d '.$weather->weatherinfo->ptime))> $nowtime)
					//break;
				$sql='insert ignore weather (cityid,temp1,temp2,weather,img1,img2,ptime,day,addtime) values ("'.$v['cityid'].'","'.$weather->weatherinfo->temp1.'","'.$weather->weatherinfo->temp2.'","'.$weather->weatherinfo->weather.'","'.$weather->weatherinfo->img1.'","'.$weather->weatherinfo->img2.'","'.$weather->weatherinfo->ptime.'","'.date('Ymd',$nowtime).'","'.$nowtime.'")';
				$pmurl='http://pm25.in/api/querys/pm2_5.json?city='.$v['cityname'].'&token=5j1znBVAsnSf5xQyNQyq&stations=no';//查询空气质量
				$pm25=json_decode(file_get_contents($pmurl));
				$result2 = $this->_dbAdapter->query ($sql);//插入
				if(empty($pm25->error)){
					if($pm25){
						$ptime=substr($pm25[0]->time_point,11,5); 
						$sql='insert weather_pm (cityid,ptime,day,addtime,pm2_5,quality) values ("'.$v['cityid'].'","'.$ptime.'","'.date('Ymd',$nowtime).'","'.$nowtime.'","'.$pm25[0]->pm2_5.'","'.$pm25[0]->quality.'")';
						$result1 = $this->_dbAdapter->query ($sql);//插入
					}
				}
				
				
				if($result2)
					$is=1;
				
			}
			$nowdate=date('Ymd');
			if($is)
				return 1;
				//$this->feedback ('刷新天气成功,请刷新页面查看', 'note', '/'.$this->controller, 'warning' ,3);
				//刷新天气成功
			else
				throw new Exception ( 'is_news' );

		} catch ( Exception $e ) {
			return $e->getMessage ();
			//$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		
		throw new Exception ( 'Invalid method' );
		
		// all other methods throw an exception
		throw new Exception ( 'Invalid method "' . $method . '" called' );
	}

}

